package licode.unisop.authcenter.adapter.web;

import cn.hutool.core.bean.BeanUtil;
import licode.unisop.client.api.auth.SopUserAuthQuery;
import licode.unisop.client.info.SopTokenInfo;
import licode.unisop.client.utils.SopRequestParse;
import licode.unisop.client.vo.SopAuthBrief;
import licode.unisop.types.dp.UserAuthIn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 定义统一认证平台中心领域相关接口
 *
 * @author licode
 */
@Slf4j
@RestController
public class CenterBaseController {
    @Resource
    private HttpServletRequest request;

    @Resource
    private SopUserAuthQuery sopUserAuthQuery;

    public UserAuthIn getAuthIn() {
        SopTokenInfo authToken;
        SopAuthBrief authBrief = null;

        authToken = SopRequestParse.getAccessTokenInfo(request);
        if (null != authToken) {
            authBrief = sopUserAuthQuery.getAuthBrief(authToken);
        }

        if (null == authBrief) {
            return UserAuthIn.builder()
                    .realmId("internal")
                    .platformId("internal")
                    .userId("system")
                    .terminalId("default")
                    .tenantId("")
                    .authType("")
                    .roleCodes("")
                    .roleNames("")
                    .build();
        } else {
            return BeanUtil.copyProperties(authBrief, UserAuthIn.class);
        }
    }
}
